<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>题目管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">

    <link rel="stylesheet" href="__PUBLIC__/BeginnerAdmin/plugins/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="__PUBLIC__/BeginnerAdmin/css/global.css" media="all">
    <link rel="stylesheet" href="__PUBLIC__/BeginnerAdmin/plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="__PUBLIC__/BeginnerAdmin/css/table.css"/>
</head>
<body>
<form>
    <div class="admin-main">
        <blockquote class="layui-elem-quote">

            <a href="javascript:;" class="layui-btn" id="add" onclick="NewTopic('<{$assignmentid}>');">
                <i class="layui-icon">&#xe608;</i> 添加题目
            </a>
            <div class="layui-inline" style="width: 100px;">
                <a href="javascript:;" id="TitleNumberManageButton" class="layui-btn layui-btn-primary">大题号管理</a>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label"></label>
                <div class="layui-input-inline">
                    <input type="text" name="so" lay-verify="so" autocomplete="off" class="layui-input"
                           placeholder="请输入要搜索的内容">
                </div>
                <a href="javascript:;" class="layui-btn" id="search">
                    <i class="layui-icon">&#xe615;</i> 搜索
                </a>
        </blockquote>

        <fieldset class="layui-elem-field">
            <legend>题目列表</legend>
            <div class="layui-field-box layui-form">
                <table id="table" lay-filter="table"></table>

                <div class="layui-form-item">
                    <div class="layui-inline">
                        <div class="layui-input-inline" style="width: 200px;">
                            <select name="operation" lay-filter="operation">
                                <option value="">操作</option>
                                <option value="delect">删除</option>
                                <option value="update">更改大题号</option>
                            </select>
                        </div>
                    </div>
                </div>
            </div>

        </fieldset>
        <div class="admin-table-page">
            <div id="paged" class="page">

            </div>
        </div>
    </div>
</form>
<div id="updataQidclick" style="display: none;">
    <form class="layui-form">
        <div style="margin: 10px 10px auto 10px">
            <select name="updataSelectclick" id="updataSelectclick" lay-filter="updataSelectclick" lay-verify="">
                <option value="">选择所属大题</option>
                <foreach name="exam_question_number" item="vo">
                    <option value="<{$vo.id}>"><{$vo.title_explain|mb_substr=###,0,6,'utf-8'}></option>
                </foreach>
            </select>
        </div>
    </form>
</div>
<div id="updataQid" style="display: none;">
    <form class="layui-form">
        <div style="margin: 10px 10px auto 10px">
            <select name="updataSelect" id="updataSelect" lay-filter="updataSelect" lay-verify="">
                <option value="">选择所属大题</option>
                <foreach name="exam_question_number" item="vo">
                    <option value="<{$vo.id}>"><{$vo.title_explain|mb_substr=###,0,6,'utf-8'}></option>
                </foreach>
            </select>
        </div>
    </form>
</div>
<script type="text/javascript" src="__PUBLIC__/BeginnerAdmin/plugins/layui/layui.js"></script>
<script src="__PUBLIC__/Jquery/Pulgin/jquery-1.9.1.js"></script>

<script>
    var SelectTitleNumber;
    var updataTitleNumber;
    layui.use(['layer', 'table', 'form'], function () {
        var $ = layui.jquery,
            layer = layui.layer,
            table = layui.table,
            form = layui.form;
        var tableloding = table.render({
            elem: '#table',
            url: "<{:U('TestQuestions/ManageSelect')}>",
            where: {assignmentid: '<{$assignmentid}>'},
            method: 'post',
            page: true,
            cols: [[
                {field: 'topicid', fixed: 'left', title: 'ID', width: 40, checkbox: true}
                ,{field: 'title_explain',fixed: 'left', title: '所属大题', width: 110, toolbar: '#TitleExplain'}
                , {field: 'sequence', fixed: 'left', title: '排序', width: 80, edit: 'text', sort: true}
                , {field: 'number', fixed: 'left', title: '题号', width: 80, edit: 'text', sort: true}
                , {field: 'body', title: '题目', width: 450}
                , {field: 'type', title: '类型', width: 80, templet: '#typeTpl'}
                , {field: 'score', title: '分数', width: 60 ,edit: 'text'}

                , {field: 'knowledge', title: '知识点'}
                , {fixed: '', fixed: 'right', width: 100, title: '操作', toolbar: '#barDemo'}
            ]]
        });

        //选择操作
        form.on('select(operation)', function (data) {
            var checkStatus = table.checkStatus('table');
            if (checkStatus.data.length < 1 && data.value != "") {
                layer.msg('您未选择任何数据');
            }

            if (checkStatus.data.length != 0) {
                if (data.value == 'update') {
                    console.log(checkStatus.data);
                    layer.open({
                        type: 1,
                        title: false,
                        area: ['230px', '410px'],
                        content: $('#updataQid')
                    });
                } else if (data.value == 'delect') {
                    var arr = new Array();
                    for (var i = 0; i < checkStatus.data.length; i++) {
                        arr[i] = checkStatus.data[i].topicid;
                    }

                    layer.confirm('真的删除行么', function (index) {
                        $.post("<{:U('TestQuestions/Delete')}>",
                            {assignmentid: '<{$assignmentid}>', "topicid": arr},
                            function (date) {
                                if (date.code == 200) {
                                    layer.msg('删除成功！');
                                } else {
                                    layer.alert(date.msg);
                                }
                            }, 'json');
                        layer.load(2);
                        layer.close(index);
                        setTimeout('settime()', 1500);//延时执行，让人看得更清楚
                        //向服务端发送删除指令
                    });
                }
            }
        });
        //批量选择更新大题号
        form.on('select(updataSelect)', function (data) {
            console.log(data.elem); //得到select原始DOM对象
            console.log(data.value); //得到被选中的值
            console.log(data.othis); //得到美化后的DOM对象
            var checkStatus = table.checkStatus('table');
            var db = new Array();
            for (var i = 0; i < checkStatus.data.length; i++) {
                db[i] = checkStatus.data[i].topicTitleNumber;
            }
            if (data.value != '' && data.value != null) {
                updataQidFun(db, data.value);
                //layer.msg('成功');
            }
        });
        //单个更改题号数据
        form.on('select(updataSelectclick)', function (data) {
            console.log(data.elem); //得到select原始DOM对象
            console.log(data.value); //得到被选中的值
            console.log(data.othis); //得到美化后的DOM对象

            if (data.value != '' && data.value != null) {
                var tmp = new Array();
                tmp[0] = updataTitleNumber;
                updataQidFun(tmp, data.value);
            }
        });

        table.on('tool(table)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent == 'edit') {
                updataTitleNumber = data.topicTitleNumber;
                //layer.msg(updataTitleNumber);
                layer.open({
                    type: 1,
                    title: false,
                    area: ['230px', '410px'],
                    content: $('#updataQidclick')
                });
            } else if (layEvent === 'delect') { //删除
                layer.confirm('真的删除行么', function (index) {
                    $.post("<{:U('TestQuestions/Delete')}>", {
                        assignmentid: '<{$assignmentid}>',
                        "topicid[0]": data.topicid
                    }, function (date) {
                        if (date.code == 200) {
                            layer.msg('删除成功！');
                            obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                        } else {
                            layer.alert('删除失败！');
                        }
                    }, 'json');


                    layer.close(index);
                    //向服务端发送删除指令
                });
                console.log(obj);
            } else if (layEvent === 'edit') { //编辑
                //do something

                //同步更新缓存对应的值
                obj.update({
                    username: '123'
                    , title: 'xxx'
                });
            }
        });

        table.on('edit(table)', function (obj) {
            console.log(obj.value); //得到修改后的值
            console.log(obj.field); //当前编辑的字段名
            console.log(obj.data); //所在行的所有相关数据

            if (obj.field == 'sequence' || obj.field == 'number'||obj.field == 'score') {
                if (!isNaN(obj.value)) {
                    var db;
                    if (obj.field == 'sequence') {
                        db = {topicid: obj.data.topicid, sequence: obj.data.sequence};
                    } else if (obj.field == 'number') {
                        db = {topicid: obj.data.topicid, number: obj.data.number};
                    } else if (obj.field == 'score') {
                        db = {topicid: obj.data.topicid, score: obj.data.score};
                    } else {
                        layer.msg('数据不正确！');
                        return false;
                    }
                    $.post("<{:U('TestQuestions/UpdataSequenceAndnumber')}>", db, function (date) {
                        if (date.code != 200) {
                            layer.alert(data.msg);
                            tableloding.reload({
                                where: { //设定异步数据接口的额外参数，任意设
                                    assignmentid: '<{$assignmentid}>'

                                }
                            });
                        }
                    }, 'json');
                } else {
                    obj.data[obj.field] = 0;
                    layer.msg('排序只能输入数字，该排序修改失败');
                }

            }

        });


    });

    $("#TitleNumberManageButton").on('click', function () {
        layer.open({
            type: 2,
            title: "管理题号",
            area: ['640px', '520px'],
            content: '<{:U('TestQuestions/TitleNumberManage',array('assignmentid'=>$assignmentid))}>',
            cancel: function (index, layero) {
                layer.load(1);
                window.location.reload(true);
            }
        });
    });
    function NewTopic(assignmentid) {
        layer.open({
            title: '添加题目',
            maxmin: false,
            type: 2,
            id: 1,
            content: "<{:U('TestQuestions/NewTopicUI',array('assignmentid'=>$assignmentid))}>",
            area: ['850px', '500px'],
            end: function (index, layero) {
                settime();
            }
        });
    }

    function settime() {
        layer.load(2);
        location.reload(true);
    }

    function SelectTitleNumberFun() {

        $.post("<{:U('TestQuestions/SelectTitleNumber')}>",
            {assignmentid: '<{$assignmentid}>'},
            function (date) {
                if (date.code == 200) {
                    SelectTitleNumber = date.data;
                } else {
                    layer.msg('题号初始化失败，该页面将不能正常工作！');
                }
            }, 'json');
    }


    function updataQidFun(Qid, TitleNumber) {
        $.post('Admin_TestQuestions_UpdateTopicTitleNumber.html', {
            assignmentid: "<{$assignmentid}>",
            TitleName: TitleNumber,
            tpoicId: Qid
        }, function (ret) {
            if (ret.code == 200) {
                layer.msg('修改成功');
                setTimeout('settime()', 1500);//延时执行，让人看得更清楚
            } else {
                layer.alert(ret.msg);
            }
        }, 'json');
    }
</script>
<script type="text/html" id="typeTpl">
    {{#  if(d.type == 1){ }}
    单选
    {{#  } else if(d.type == 2){ }}
    多选
    {{#  } else if(d.type == 3){ }}
        填空题
    {{#  } else if(d.type == 4){ }}
        简答题
    {{#  } else if(d.type == 5){ }}
        内置题
    {{#  } else { }}
    未定义
    {{#  } }}
</script>
<script type="text/html" id="TitleExplain">

    <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="edit">
        {{# if(d.title_explain != null){ }}
        {{ d.title_explain}}
        {{# } else { }}
        未选择
        {{# } }}
    </a>
</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="delect"><i class="layui-icon">&#xe640;</i>删除</a>
</script>
</body>
</html>